<?php
/**
 * tvb log simple text pattern
 * 
 * @author William Hu <william.hu@achievo.com>
 * @version 1.0
 * @package TVBLog
 */
require_once 'TVB/Logger/Pattern/Interface.php';
class TVB_Logger_Pattern_SimpleText implements TVB_Logger_Pattern_Interface
{
    /**
     * @var string
     */
    protected $_format;

    const DEFAULT_FORMAT = '[%when%] [%category%] [%level%] [%who%] [%request%] [%note%] %result% ';
	
    /**
     * Class constructor
     *
     * @param  null|string  $format  Format specifier for log messages
     * @throws TVB_ILog_Exception
     */
    public function __construct($format = null)
    {
        if ($format === null) {
            $format = self::DEFAULT_FORMAT . PHP_EOL;
        }

        if (! is_string($format)) {
            require_once 'TVB/Logger/Exception.php';
            throw new TVB_Logger_Exception('Format must be a string');
        }

        $this->_format = $format;
    }

    /**
     * Formats data into a single line to be written by the writer.
     *
     * @param  array    $event    event data
     * @return string             formatted line to write to the log
     */
    public function toString($event)
    {
        $output = $this->_format;
        foreach ($event as $name => $value) {
            if ((is_object($value) && !method_exists($value,'__toString')) || is_array($value)) {
                $value = gettype($value);
            }
            $output = str_replace("%$name%", $value, $output);
        }
        return $output;
    }
}
?>

